package com.jinlejia.feedback.weight;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jinlejia.feedback.R;
import com.weight.CircleProgressBar;

/**
 * 意见反馈-文件下载控件
 */

public class FbDownloadFileView extends LinearLayout {
    public FbDownloadFileView(Context context) {
        this(context,null);
    }

    public FbDownloadFileView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public FbDownloadFileView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public FbDownloadFileView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs, defStyleAttr);
    }
    private String downloadUrl;
    private String fileName;
    private CircleProgressBar progressBar;
    public void setDownloadUrl(String downloadUrl) {
        this.downloadUrl = downloadUrl;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
        fileNameTextView.setText(fileName);
    }

    public String getFileName() {
        return fileName;
    }

    public String getDownloadUrl() {
        return downloadUrl;
    }

    private DownloadListener downloadListener;

    public void setDownloadListener(DownloadListener downloadListener) {
        this.downloadListener = downloadListener;
    }

    private TextView fileNameTextView;
    private void init(Context context, AttributeSet attrs, int defStyleAttr){
        LayoutInflater.from(context).inflate(R.layout.fb_weight_download, this, true);
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (downloadListener!=null){
                    downloadListener.download(progressBar,downloadUrl);
                }
            }
        });
        fileNameTextView = findViewById(R.id.fileNameTextView);
        progressBar = findViewById(R.id.progressBar);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.FbDownloadFileView);
        fileName = typedArray.getString(R.styleable.FbDownloadFileView_fbFileName);
        typedArray.recycle();
        fileNameTextView.setText(fileName);
    }

    public interface DownloadListener{
        void download(CircleProgressBar progressBar,String downloadUrl);
    }
}
